<?php
/* ========== ########## ========== ########## ========== ########## ========== */
/*                                                                              */
/*   WHMCS v4.1.2 NULLED R4 by FLIPMODE  [ BAD SYNTAX ]                         */
/*                                                                              */
/*                             INTERNAL BAD SYNTAX NULLED VERSION: 2.5.9.6 R4   */
/*                                                                              */
/*   ALL files have been analysed, cleaned and nulled of:                       */
/*     - Call Backs  (100%)                                                     */
/*     - Link Backs (95%)                                                       */
/*     - Licensing  (100%)                                                      */
/*     - Remote Links + URL's                                                   */
/*                                                                              */
/*   WHMCS v4.1.2 RELEASED: 4th November 2009                                   */
/*    -> FIRST v4.1.2 NULL R1: 19th November 2009                               */
/*    -> UPDATED v4.1.2 NULL R4: 27th November 2009                             */
/*                                                                              */
/*   TESTING + VERIFICATION::                                                   */
/*     Files Cleaned, Nulled, Checked, Verified By:                             */
/*      -> FLIPMODE!                                                            */
/*     Decoded By:                                                              */
/*      -> FLIPMODE! + Unknown Decoder                                          */
/*     Tested By:                                                               */
/*      -> FLIPMODE!, LeetWolf, and 12 Anonymous Users...                       */
/*     WHMCS 4.1.2 Supplied By:                                                 */
/*      -> WHM CompleteShit Coder @ WHMCS.com                                   */
/*                                                                              */
/*                                                                              */
/*   IMPORTANT NOTE::                                                           */
/*   PLEASE NOTE THIS IS FOR TRIAL PURPOSES ONLY!                               */
/*                          LIKE IT? THEN BUY IT!                               */
/*                                                                              */
/* ========== ########## ========== ########## ========== ########## ========== */

function downloadlogin ()
{
	global $CONFIG;
	global $_LANG;
	global $smarty;
	global $type;
	global $id;
	$pagetitle = $_LANG['downloadstitle'];
	$breadcrumbnav = '<a href="' . $CONFIG['SystemURL'] . '/index.php">' . $_LANG['globalsystemname'] . '</a> > <a href="' . $CONFIG['SystemURL'] . '/downloads.php">' . $_LANG['downloadstitle'] . '</a>';
	initialiseclientarea ($pagetitle, $pageicon, $breadcrumbnav);
	$goto = 'download';
	require 'login.php';
}

define ('CLIENTAREA', true);
require 'dbconnect.php';
require 'includes/functions.php';
require 'includes/clientareafunctions.php';
$fileurl = '';
$i = (int)$i;
if ($type == 'i')
{
	$result = select_query ('tblinvoices', '', array ('id' => $id));
	$data = mysql_fetch_array ($result);
	$invoiceid = $data['id'];
	$invoicenum = $data['invoicenum'];
	$userid = $data['userid'];
	if ((!$_SESSION['adminid'] AND $_SESSION['uid'] != $userid))
	{
		downloadlogin ();
	}

	if (!$invoicenum)
	{
		$invoicenum = $invoiceid;
	}

	require 'includes/clientfunctions.php';
	require 'includes/countries.php';
	require 'includes/invoicefunctions.php';
	require 'includes/tcpdf.php';
	$pdfdata = pdfinvoice ($id);
	header ('Pragma: public');
	header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header ('Last-Modified: ' . gmdate ('D, d M Y H:i:s') . ' GMT');
	header ('Cache-Control: must-revalidate, post-check=0, pre-check=0, private');
	header ('Cache-Control: private', false);
	header ('Content-Type: application/octet-stream');
	header ('Content-Disposition: attachment; filename="' . $invoicenum . '.pdf"');
	header ('Content-Transfer-Encoding: binary');
	echo $pdfdata;
	exit ();
	return 1;
}

if ($type == 'a')
{
	$result = select_query ('tbltickets', 'userid,attachment', array ('id' => $id));
	$data = mysql_fetch_array ($result);
	$userid = $data['userid'];
	$filename = $data['attachment'];
	$file = explode ('|', $filename);
	$filename = $file[$i];
	$fileurl = $attachments_dir . $filename;
	$filename = substr ($filename, 7);
	if (($userid AND ($userid != $_SESSION['uid'] AND !$_SESSION['adminid'])))
	{
		downloadlogin ();
	}
}
else
{
	if ($type == 'ar')
	{
		$result = select_query ('tblticketreplies', 'userid,attachment', array ('id' => $id));
		$data = mysql_fetch_array ($result);
		$userid = $data['userid'];
		$filename = $data['attachment'];
		$file = explode ('|', $filename);
		$filename = $file[$i];
		$fileurl = $attachments_dir . $filename;
		$filename = substr ($filename, 7);
		if (($userid AND ($userid != $_SESSION['uid'] AND !$_SESSION['adminid'])))
		{
			downloadlogin ();
		}
	}
	else
	{
		if ($type == 'd')
		{
			$result = select_query ('tbldownloads', '', array ('id' => $id));
			$data = mysql_fetch_array ($result);
			$filename = $data['location'];
			$clientsonly = $data['clientsonly'];
			$productdownload = $data['productdownload'];
			if ($productdownload)
			{
				$result = select_query ('tblhosting', 'DISTINCT tblproducts.id,tblproducts.downloads', array ('userid' => $_SESSION['uid'], 'tblhosting.domainstatus' => 'Active'), '', '', '', 'tblproducts ON tblproducts.id=tblhosting.packageid');
				while ($data = mysql_fetch_array ($result))
				{
					$downloads = $data['downloads'];
					$downloads = unserialize ($downloads);
					if (($downloads AND in_array ($id, $downloads)))
					{
						$allowedtodownload = true;
						continue;
					}
				}

				if (!$allowedtodownload)
				{
					echo $_LANG['downloadpurchaserequired'];
					exit ();
				}
			}

			if (($clientsonly AND !$_SESSION['uid']))
			{
				downloadlogin ();
			}

			if (substr ($filename, 0, 4) == 'http')
			{
				header ('' . 'Location: ' . $filename);
				exit ();
			}
			else
			{
				$fileurl = $downloads_dir . $filename;
			}

			update_query ('tbldownloads', array ('downloads' => '+1'), array ('id' => $id));
		}
		else
		{
			if ($type == 'f')
			{
				$result = select_query ('tblclientsfiles', 'userid,filename,adminonly', array ('id' => $id));
				$data = mysql_fetch_array ($result);
				$userid = $data['userid'];
				$filename = $data['filename'];
				$adminonly = $data['adminonly'];
				$fileurl = $attachments_dir . $filename;
				$filename = substr ($filename, 11);
				if (($userid != $_SESSION['uid'] AND !$_SESSION['adminid']))
				{
					downloadlogin ();
				}

				if ((!$_SESSION['adminid'] AND $adminonly))
				{
					exit ('Permission Denied');
				}
			}
		}
	}
}

if ((!$filename OR !is_file ($fileurl)))
{
	exit ('File not found. Please contact support.');
}

header ('Pragma: public');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: ' . gmdate ('D, d M Y H:i:s') . ' GMT');
header ('Cache-Control: must-revalidate, post-check=0, pre-check=0, private');
header ('Content-Type: application/octet-stream');
header ('Content-Disposition: attachment; filename="' . $filename . '"');
header ('Content-Transfer-Encoding: binary');
header ('Content-Length: ' . filesize ($fileurl) . '');
readfile ($fileurl);
?>